How odd this function is? It works in a test project, however, it goes wrong in my project?(Windows Socket) [closed]
Posted
by
user67449
on Programmers
See other posts from Programmers
or by user67449
Published on 2012-10-11T12:56:11Z
Indexed on
2012/10/11
15:47 UTC
Read the original article
Hit count: 127
c++
int SockSend(DataPack &dataPack, SOCKET &sock, char *sockBuf){
int bytesLeft=0, bytesSend=0;
int idx=0;
bytesLeft=sizeof(dataPack);
// ?DataPack?????sockBuf???
memcpy(sockBuf, &dataPack, sizeof(dataPack));
while(bytesLeft>0){
memset(sockBuf, 0, sizeof(sockBuf));
bytesSend=send(sock, &sockBuf[idx], bytesLeft, 0);
cout<<"??send()??, bytesSend: "<<bytesSend<<endl;
if(bytesSend==SOCKET_ERROR){
cout<<"Error at send()."<<endl;
cout<<"Error # "<<WSAGetLastError()<<" happened."<<endl;
return 1;
}
bytesLeft-=bytesSend;
idx+=bytesSend;
}
cout<<"DataPack ???????"<<endl;
return 0;
}
This is the function I defined, which is used to send a user_defined structure DataPack. My code in test project is as follows:
char sendBuf[100000];
int res=SockSend(dataPack, sockConn, sendBuf);
if(res==1){
cout<<"SockSend()???"<<endl;
}else{
cout<<"SockSend()???"<<endl;
}
My code in my current project is:
err=SockSend(dataPackSend, sockConn, sockBuf);
if(err==1){
cout<<"SockSend()??"<<endl;
exit(0);
}else{
cout<<"??? "<<dataPackSend.packNum<<" ?DataPack(??)"<<endl;
}
Can you tell me where does this function go wrong? I will be appreciated for you answer.
© Programmers or respective owner